package cn.taokuaidi.wangwang.client.model;

import java.util.List;

import cn.taokuaidi.wangwang.client.util.JavaScriptUtils;

import com.extjs.gxt.ui.client.data.BaseModelData;

public class OrderDO extends BaseModelData {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    public String getId() {
        Object obj = get("id");
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    public ExpressDO getExpress() {
        return get("express");
    }

    public void setExpress(ExpressDO express) {
        set("express", express);
    }

    private String getToSellerMsg() {
        String sellerNick = get("seller_nick");
        String title = get("title");
        String msg = "hi," + sellerNick + "\n\n" + "我在您处购买的:" + title;
        ExpressDO express = getExpress();
        if (express != null) {
            List<StatusInfo> statuses = express.getStatuses();
            if (statuses != null && statuses.size() > 0) {
                msg = msg + "\n\n当前快递信息如下:\n\n";
                msg = msg + statuses.get(0).getHappenTime() + statuses.get(0).getInfo();
                msg = msg + "\n\n这是为什么啊？";
            }
        }
        return format(msg);
    }

    private String getToBuyerMsg() {
        String buyerNick = get("buyer_nick");
        String title = get("title");
        String msg = "hi," + buyerNick + "\n\n" + "您在我处购买的:" + title;
        ExpressDO express = getExpress();
        if (express != null) {
            List<StatusInfo> statuses = express.getStatuses();
            if (statuses != null && statuses.size() > 0) {
                msg = msg + "\n\n当前快递信息如下:\n\n";
                msg = msg + statuses.get(0).getHappenTime() + "" + statuses.get(0).getInfo();
                msg = msg + "\n\n欢迎您下一次再次光临";
            }
        }
        return format(msg);

    }

    private String format(String str) {
        str = JavaScriptUtils.javaScriptEscape(str);
        return str;
    }

    public void setNickImage() {
        String sellerNick = get("seller_nick");
        String buyerNick = get("buyer_nick");
        String sendMsgToSeller = "<a href=javascript:sendMsg(\"" + sellerNick + "\",\"" + getToSellerMsg()
                                 + "\")><img src='/images/ww1.png' border=0></a>" + sellerNick;
        String sendMsgToBuyer = "<a href=javascript:sendMsg(\"" + buyerNick + "\",\"" + getToBuyerMsg()
                                + "\")><img src='/images/ww1.png' border=0></a>" + buyerNick;
        set("seller_nick_msg", sendMsgToSeller);
        set("buyer_nick_msg", sendMsgToBuyer);
    }

    public String getLastStatus() {
        Object obj = get("last_status");
        if (obj == null) {
            // 获得总价信息
            Object numObject = this.get("num");
            Object priceObject = this.get("price");
            if (numObject != null && priceObject != null) {
                try {
                    double total = new Integer(numObject.toString()).intValue()
                                   * new Double(priceObject.toString()).doubleValue();
                    set("total", total);
                } catch (Exception error) {

                }
            }
            String value = "";
            ExpressDO express = getExpress();
            if (express == null) {
                value = "<img src='/images/error.jpg' width='15' height='15' title='尚未设置快递信息'>";
            } else {
                AgentDO agent = express.getAgent();
                if (agent != null) {
                    String str = agent.getName() + ":" + express.getNo();
                    if (!agent.isSupport()) {
                        str = "快递公司:" + agent.getName() + ",尚未支持";
                        value = "<img src='/images/error.jpg' width='15' height='15' title='" + str + "'>";
                    }
                    set("express_info", str);
                } else {
                    set("express_info", "尚未设置");
                    value = "<img src='/images/error.jpg' width='15' height='15' title='尚未设置快递信息'>";
                }
                if (express.isNotExist()) {
                    value = "<img src='/images/error.jpg' width='15' height='15' title='快递号不存在，请检查是否正确'>";
                } else if (express.isEnd()) {
                    value = "<img src='/images/done.gif' width='15' height='15' title='收货完成'>";
                } else {
                    List<StatusInfo> statuses = express.getStatuses();
                    if (statuses == null) {
                        value = "<img src='/images/doing.gif' width='15' height='15' title='尚未发货,一般当天发货'>";
                    } else {
                        if (statuses.size() > 0) {
                            StatusInfo info = statuses.get(0);
                            value = "当前情况:" + info.getHappenTime() + " " + info.getInfo();
                            value = "<img src='/images/doing.gif' width='15' height='15' title='" + value + "'>";

                        } else {
                            value = "<img src='/images/doing.gif' width='15' height='15' title='尚未发货,一般当天发货'>";
                        }
                    }
                }
            }

            set("last_status", value);
            obj = value;
        }
        return (String) obj;
    }

    public String getDetail() {
        Object obj = get("detail");
        if (obj == null) {
            StringBuffer retVal = new StringBuffer("<table>");
            ExpressDO express = getExpress();
            if (express != null) {
                retVal.append("<td>");
                retVal.append("<table>");
                retVal.append("<tr>");
                retVal.append("<td>");
                retVal.append("快递号:" + express.getNo());
                retVal.append("</td>");
                retVal.append("</tr>");
                List<StatusInfo> status = express.getStatuses();
                if (status != null) {
                    retVal.append("<tr>");
                    retVal.append("<td>");
                    retVal.append("详细信息:");
                    retVal.append("</td>");
                    retVal.append("</tr>");
                    for (StatusInfo item : status) {
                        retVal.append("<tr>");
                        retVal.append("<td>");
                        retVal.append(item.getHappenTime() + "   " + item.getInfo());
                        retVal.append("</td>");
                        retVal.append("<tr>");

                    }
                }
                retVal.append("</table>");
                retVal = retVal.append("</td>");
            }
            retVal.append("</tr><tr><td><img width='70' height='70' src='" + get("pic_path") + "'/></td></table>");
            set("detail", retVal.toString());
            obj = retVal.toString();
        }
        return (String) obj;
    }
}
